﻿using System.Text.Json.Serialization;
using Transponder_Core.DataTarget;

namespace Transponder_Core.DataSource
{
    public class RealTimeElectricalParameters
    {

        public RealTimeElectricalParameters()
        {
            
        }

        public string imei { get; set; }

        public decimal Real_UA { get; set; }
        public decimal Real_UB { get; set; }
        public decimal Real_UC { get; set; }

             public decimal Real_IA { get; set; }
        public decimal Real_IB { get; set; }
        public decimal Real_IC
        {
            get; set;
        }
        public decimal Real_LD
        {
            get; set;
        }

        public decimal Real_TA
        {
            get; set;
        }
        public decimal Real_TB
        {
            get; set;
        }
        public decimal Real_TC
        {
            get; set;
        }
        public decimal Real_TN
        {
            get; set;
        }
        [JsonIgnore]
        public DateTime eventTime {  get; set; }
         
        public decimal YGGL_P { get; set; }
        public decimal YGGL_PA { get; set; }
        public decimal YGGL_PB { get; set; }
        public decimal YGGL_PC { get; set; }
        public decimal WGGL_Q { get; set; }
        public decimal WGGL_QA { get; set; }
        public decimal WGGL_QB { get; set; }
        public decimal WGGL_QC { get; set; }
        public decimal SZGL_S { get; set; }
        public decimal SZGL_SA { get; set; }
        public decimal SZGL_SB { get; set; }
        public decimal SZGL_SC { get; set; }
        public decimal electric_fr { get; set; }
        public decimal ActiveEnergyImportInPhaseL1 { get; set; }
        public decimal ActiveEnergyImportInPhaseL2 { get; set; }
        public decimal ActiveEnergyImportInPhaseL3 { get; set; }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="json"></param>
        /// <returns></returns>
        public static RealTimeElectricalParameters FromJson(string json)
        {
            try
            {
                var obj = System.Text.Json.JsonSerializer.Deserialize<DataSource.RealTimeElectricalParameters>(json);
                return obj;
            }
            catch
            {
                return null;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public  string ToJson()
        {
            try
            {
                var obj = System.Text.Json.JsonSerializer.Serialize<DataSource.RealTimeElectricalParameters>(this);
                return obj;
            }
            catch
            {
                return string.Empty;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public Payload ToPayload()
        {
            var ts = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();

            var payload = new Payload();

            payload.ts = ts;

            var device = new Device();

            device.ts = ts;

            MeasurePoint uaMeasurePoint = new MeasurePoint();
            uaMeasurePoint.m = MeasurePointKeys.Ua.ToString();
            uaMeasurePoint.v = (decimal)this.Real_UA;
            uaMeasurePoint.ts = ts;
            device.d.Add(uaMeasurePoint);


            MeasurePoint ubMeasurePoint = new MeasurePoint();
            ubMeasurePoint.m = MeasurePointKeys.Ub.ToString();
            ubMeasurePoint.v = (decimal)this.Real_UB;
            ubMeasurePoint.ts = ts;
            device.d.Add(ubMeasurePoint);

            MeasurePoint ucMeasurePoint = new MeasurePoint();
            ucMeasurePoint.m = MeasurePointKeys.Ub.ToString();
            ucMeasurePoint.v = (decimal)this.Real_UC;
            ucMeasurePoint.ts = ts;
            device.d.Add(ucMeasurePoint);

            MeasurePoint iaMeasurePoint = new MeasurePoint();
            iaMeasurePoint.m = MeasurePointKeys.Ia.ToString();
            iaMeasurePoint.v = (decimal)this.Real_IA;
            iaMeasurePoint.ts = ts;
            device.d.Add(iaMeasurePoint);


            MeasurePoint ibMeasurePoint = new MeasurePoint();
            ibMeasurePoint.m = MeasurePointKeys.Ib.ToString();
            ibMeasurePoint.v = (decimal)this.Real_IB;
            ibMeasurePoint.ts = ts;
            device.d.Add(ibMeasurePoint);

            MeasurePoint icMeasurePoint = new MeasurePoint();
            icMeasurePoint.m = MeasurePointKeys.Ic.ToString();
            icMeasurePoint.v = (decimal)this.Real_IC;
            icMeasurePoint.ts = ts;
            device.d.Add(icMeasurePoint);
             


            MeasurePoint taMeasurePoint = new MeasurePoint();
            taMeasurePoint.m = MeasurePointKeys.Ta.ToString();
            taMeasurePoint.v = (decimal)this.Real_TA;
            taMeasurePoint.ts = ts;
            device.d.Add(taMeasurePoint);

            MeasurePoint tbMeasurePoint = new MeasurePoint();
            tbMeasurePoint.m = MeasurePointKeys.Tb.ToString();
            tbMeasurePoint.v = (decimal)this.Real_TB;
            tbMeasurePoint.ts = ts;
            device.d.Add(tbMeasurePoint);

            MeasurePoint tcMeasurePoint = new MeasurePoint();
            tcMeasurePoint.m = MeasurePointKeys.Tb.ToString();
            tcMeasurePoint.v = (decimal)this.Real_TC;
            tcMeasurePoint.ts = ts;
            device.d.Add(tcMeasurePoint);


            MeasurePoint tnMeasurePoint = new MeasurePoint();
            tnMeasurePoint.m = MeasurePointKeys.Tn.ToString();
            tnMeasurePoint.v = (decimal)this.Real_TN;
            tnMeasurePoint.ts = ts;
            device.d.Add(tnMeasurePoint);



            MeasurePoint pMeasurePoint = new MeasurePoint();
            pMeasurePoint.m = MeasurePointKeys.P.ToString();
            pMeasurePoint.v = (decimal)this.YGGL_P;
            pMeasurePoint.ts = ts;
            device.d.Add(pMeasurePoint);

            MeasurePoint paMeasurePoint = new MeasurePoint();
            paMeasurePoint.m = MeasurePointKeys.Pa.ToString();
            paMeasurePoint.v = (decimal)this.YGGL_PA;
            paMeasurePoint.ts = ts;
            device.d.Add(paMeasurePoint);


            MeasurePoint pbMeasurePoint = new MeasurePoint();
            pbMeasurePoint.m = MeasurePointKeys.Pb.ToString();
            pbMeasurePoint.v = (decimal)this.YGGL_PB;
            pbMeasurePoint.ts = ts;
            device.d.Add(pbMeasurePoint);


            MeasurePoint pcMeasurePoint = new MeasurePoint();
            pcMeasurePoint.m = MeasurePointKeys.Pc.ToString();
            pcMeasurePoint.v = (decimal)this.YGGL_PC;
            pcMeasurePoint.ts = ts;
            device.d.Add(pcMeasurePoint);


            MeasurePoint qMeasurePoint = new MeasurePoint();
            qMeasurePoint.m = MeasurePointKeys.Q.ToString();
            qMeasurePoint.v = (decimal)this.WGGL_Q;
            qMeasurePoint.ts = ts;
            device.d.Add(qMeasurePoint);


            MeasurePoint qaMeasurePoint = new MeasurePoint();
            qaMeasurePoint.m = MeasurePointKeys.Qa.ToString();
            qaMeasurePoint.v = (decimal)this.WGGL_QA;
            qaMeasurePoint.ts = ts;
            device.d.Add(qaMeasurePoint);
             

            MeasurePoint qbMeasurePoint = new MeasurePoint();
            qbMeasurePoint.m = MeasurePointKeys.Qb.ToString();
            qbMeasurePoint.v = (decimal)this.WGGL_QB;
            qbMeasurePoint.ts = ts;
            device.d.Add(qbMeasurePoint);


            MeasurePoint qcMeasurePoint = new MeasurePoint();
            qcMeasurePoint.m = MeasurePointKeys.Qc.ToString();
            qcMeasurePoint.v = (decimal)this.WGGL_QC;
            qcMeasurePoint.ts = ts;
            device.d.Add(qcMeasurePoint);


            MeasurePoint sMeasurePoint = new MeasurePoint();
            sMeasurePoint.m = MeasurePointKeys.S.ToString();
            sMeasurePoint.v = (decimal)this.SZGL_S;
            sMeasurePoint.ts = ts;
            device.d.Add(sMeasurePoint);


            MeasurePoint saMeasurePoint = new MeasurePoint();
            saMeasurePoint.m = MeasurePointKeys.Sa.ToString();
            saMeasurePoint.v = (decimal)this.SZGL_SA;
            saMeasurePoint.ts = ts;
            device.d.Add(saMeasurePoint);


            MeasurePoint sbMeasurePoint = new MeasurePoint();
            sbMeasurePoint.m = MeasurePointKeys.Sb.ToString();
            sbMeasurePoint.v = (decimal)this.SZGL_SB;
            sbMeasurePoint.ts = ts;
            device.d.Add(sbMeasurePoint);



            MeasurePoint scMeasurePoint = new MeasurePoint();
            scMeasurePoint.m = MeasurePointKeys.Sc.ToString();
            scMeasurePoint.v = (decimal)this.SZGL_SC;
            scMeasurePoint.ts = ts;
            device.d.Add(scMeasurePoint);


            MeasurePoint uhzMeasurePoint = new MeasurePoint();
            uhzMeasurePoint.m = MeasurePointKeys.UHZ.ToString();
            uhzMeasurePoint.v = (decimal)this.electric_fr;
            uhzMeasurePoint.ts = ts;
            device.d.Add(uhzMeasurePoint);


            MeasurePoint posAPhaseIntPMeasurePoint = new MeasurePoint();
            posAPhaseIntPMeasurePoint.m = MeasurePointKeys.PosAPhaseIntP.ToString();
            posAPhaseIntPMeasurePoint.v = (decimal)this.ActiveEnergyImportInPhaseL1;
            posAPhaseIntPMeasurePoint.ts = ts;
            device.d.Add(posAPhaseIntPMeasurePoint);


            MeasurePoint posBPhaseIntPMeasurePoint = new MeasurePoint();
            posBPhaseIntPMeasurePoint.m = MeasurePointKeys.PosBPhaseIntP.ToString();
            posBPhaseIntPMeasurePoint.v = (decimal)this.ActiveEnergyImportInPhaseL2;
            posBPhaseIntPMeasurePoint.ts = ts;
            device.d.Add(posBPhaseIntPMeasurePoint);



            MeasurePoint posCPhaseIntPMeasurePoint = new MeasurePoint();
            posCPhaseIntPMeasurePoint.m = MeasurePointKeys.PosBPhaseIntP.ToString();
            posCPhaseIntPMeasurePoint.v = (decimal)this.ActiveEnergyImportInPhaseL3;
            posCPhaseIntPMeasurePoint.ts = ts;
            device.d.Add(posCPhaseIntPMeasurePoint);

            payload.devs.Add(device);
            return payload;
        }

    }
}
